package com.dcjt.firehotcarpad.model

import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.retrofit.FastLoadingObserver
import com.dcjt.firehotcarpad.entity.BaseEntity
import com.dcjt.firehotcarpad.entity.LoginResultEntity
import com.dcjt.firehotcarpad.retrofit1.repository.ApiRepository1
import com.google.gson.Gson
import com.trello.rxlifecycle2.android.ActivityEvent
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity

class LoginViewModel(app: Application) : AndroidViewModel(app) {
    val requestStatusLiveData: MutableLiveData<BaseEntity<LoginResultEntity>> = MutableLiveData()
    val baseBean = BaseEntity<LoginResultEntity>()

    fun login(account: String, password: String,context:Context,cslxdm:String){
        ApiRepository1.getInstance().Login(account,password,cslxdm)
            .compose((context as RxAppCompatActivity).bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe(object : FastLoadingObserver<LoginResultEntity>(context) {


                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    baseBean.code =  BaseEntity.err
                    baseBean.data = null
                    requestStatusLiveData.value = baseBean
                }

                override fun _onNext(entity: LoginResultEntity) {
                    LoggerManager.d(Gson().toJson(entity))

                    baseBean.code = BaseEntity.ok
                    baseBean.data = entity
                    requestStatusLiveData.value = baseBean

//                    var headerMaps =  HashMap<String, Any>()
//                    headerMaps["token"] = entity.token
//                    FastRetrofit.getInstance().setHeaders(headerMaps)
                }

            })


    }
    fun getConfigData(configType: String,context:Context){



    }
}